//
//  BaseViewController.m
//  CreativeLife
//
//  Created by qianfeng on 15-6-11.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController


- (void)viewDidLoad {
    [super viewDidLoad];
//    [self creatRefreshButton];
    [self creatTableView];
    self.view.backgroundColor=[UIColor lightGrayColor];
    if ([self.category isEqualToString:kGengDuoType]) {
        
    }else{
        [self creatRefresh];
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"MyCell"];
    [self loadDataWithUrl:nil];
}
#pragma mark -creatTableView
-(void)creatTableView{
    self.dataArr=[[NSMutableArray alloc]init];
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64-49) style:UITableViewStylePlain];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    
    [self.view addSubview:self.tableView];
    if ([self.category isEqualToString:kGengDuoType]) {
    }else{
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,0, kScreenSize.width, 30)];
        label.text=@"已经加载全部";
        label.textColor=[UIColor grayColor];
        label.textAlignment=NSTextAlignmentCenter;
        label.font=[UIFont boldSystemFontOfSize:14];
        
        self.tableView.tableFooterView=label;

    
    }
    
    
}
#pragma mark -TableView Protocol
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    
    CreativeModel *model=self.dataArr[indexPath.row];
    
    [cell showDataWithModel:model];
    return cell;
    
}


-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}
#pragma mark -creatRefreshButton
-(void)creatRefreshButton{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(0, 0, 50, 30);
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem=item;
    
    
}

-(void)btnClick:(UIButton *)button{
    NSLog(@"button Click!");
    [self creatRefresh];
}
#pragma mark -loadData

-(void)loadDataWithUrl:(NSString *)url{
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    if ([self.category isEqualToString:kSuiJiChuanYueType]) {
        NSInteger t=arc4random()%10;
        url=[NSString stringWithFormat:self.resquestUrl,t];
    }else{
        url=self.resquestUrl;
    }
    __weak typeof (self)weakSelf=self;
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"数据下载成功");
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"dict:%@",dict);
        NSArray *arr=dict[@"content"];
        
        for (NSInteger i=1; i<arr.count; i++) {
            NSDictionary *dict=arr[i];
            CreativeModel *model=[[CreativeModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            NSArray *imageArr=dict[@"image_urls"];
            NSDictionary  *imageDict=imageArr[0];
            model.middle=imageDict[@"middle"];
        
//            NSLog(@"model:%@",model.middle);
//            NSLog(@"model:%@",model.text);
            if (!model.is_ad) {
                 [weakSelf.dataArr addObject:model];
            }
           
        }
        [weakSelf.tableView reloadData];
        [weakSelf endRefresh];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据下载失败");
        NSLog(@"错误原因:%@",error);
        [weakSelf endRefresh];
    }];
    
    
}
#pragma mark -creatRefresh
-(void)creatRefresh{
   
    __weak typeof (self)weakSelf=self;
    
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefresh) {
            return ;
        }
        weakSelf.isRefresh=YES;
        [weakSelf loadDataWithUrl:nil];
    }];
    
    
}
-(void)endRefresh{
    if (self.isRefresh) {
        self.isRefresh=NO;
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }
    if (self.isLoadMore) {
        self.isLoadMore=NO;
        [self.tableView footerEndRefreshing];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
